package com.ruoyi.web.controller.ora;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ora.domain.UserWhite;
import com.ruoyi.ora.service.IUserWhiteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 白名单Controller
 * 
 * @author liuwp
 * @date 2024-12-05
 */
@RestController
@RequestMapping("/ora/user_white")
public class UserWhiteController extends BaseController
{
    @Autowired
    private IUserWhiteService userWhiteService;

    /**
     * 查询白名单列表
     */
    @PreAuthorize("@ss.hasPermi('ora:user_white:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserWhite userWhite)
    {
        startPage();
        List<UserWhite> list = userWhiteService.selectUserWhiteList(userWhite);
        return getDataTable(list);
    }

    /**
     * 导出白名单列表
     */
    @PreAuthorize("@ss.hasPermi('ora:user_white:export')")
    @Log(title = "白名单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserWhite userWhite)
    {
        List<UserWhite> list = userWhiteService.selectUserWhiteList(userWhite);
        ExcelUtil<UserWhite> util = new ExcelUtil<UserWhite>(UserWhite.class);
        util.exportExcel(response, list, "白名单数据");
    }

    /**
     * 获取白名单详细信息
     */
    @PreAuthorize("@ss.hasPermi('ora:user_white:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(userWhiteService.selectUserWhiteById(id));
    }

    /**
     * 新增白名单
     */
    @PreAuthorize("@ss.hasPermi('ora:user_white:add')")
    @Log(title = "白名单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserWhite userWhite)
    {
        return toAjax(userWhiteService.insertUserWhite(userWhite));
    }

    /**
     * 修改白名单
     */
    @PreAuthorize("@ss.hasPermi('ora:user_white:edit')")
    @Log(title = "白名单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserWhite userWhite)
    {
        return toAjax(userWhiteService.updateUserWhite(userWhite));
    }

    /**
     * 删除白名单
     */
    @PreAuthorize("@ss.hasPermi('ora:user_white:remove')")
    @Log(title = "白名单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userWhiteService.deleteUserWhiteByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('ora:user_white:list')")
    @GetMapping("/query")
    public TableDataInfo query(UserWhite userWhite)
    {
        userWhite.setUserId(SecurityUtils.getUserId());
        startPage();
        List<UserWhite> list = userWhiteService.selectUserWhiteList(userWhite);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('ora:user_white:add')")
    @Log(title = "白名单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addWhite(UserWhite userWhite, MultipartFile file)
    {

        if (file != null){
            try {
                // 上传文件路径
                String filePath = RuoYiConfig.getUploadPath();
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileName);
                userWhite.setAvatar(fileName);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        userWhite.setUserId(SecurityUtils.getUserId());
        return toAjax(userWhiteService.insertUserWhite(userWhite));
    }

    @PreAuthorize("@ss.hasPermi('ora:user_white:edit')")
    @Log(title = "白名单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editWhite(UserWhite userWhite, MultipartFile file)
    {
        if (file != null){
            try {
                // 上传文件路径
                String filePath = RuoYiConfig.getUploadPath();
                // 上传并返回新文件名称
                String fileName = FileUploadUtils.upload(filePath, file);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileName);
                userWhite.setAvatar(fileName);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        return toAjax(userWhiteService.updateUserWhite(userWhite));
    }

}
